package zhupf.test.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * BlokcingQueue：阻塞队列，是一个接口，和List、Set平级，都是collect的子接口;原则，先进先出
 * 其主要核心实现类有两个：ArrayBlockingQueue、LinkedBlockingQueue.
 * 方法大致分为四类：抛异常、boolean、阻塞、等待超时
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(3);
/*
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //队列数量为3个，满时再添加报错：IllegalStateException:Queue full
//        System.out.println(blockingQueue.add("d"));
        //remove:移除队列中的第一个元素，返回一个对象
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //队列空时，继续移除报错
//        System.out.println(blockingQueue.remove());
//检查队列的第一个值/返回对象，空的时候报错
        System.out.println(blockingQueue.element());*/

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

        System.out.println(blockingQueue.peek());



    }
}
